﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CS3230HealthTrackerSystemGUI
{
    /// <summary>
    /// @Author: Chase Cole
    /// @Version: 11/30/2011
    /// 
    /// The Validation class compiles the validation method from the various other GUI forms. 
    /// Used to check values to make sure they adhere to expected structure to be stored within the database. 
    /// </summary>
    class Validation
    {

        public void validateDiagonosis(String diagnosis)
        {
            //Removes any spaces from the name to be added.
            String currentName = diagnosis.Replace(" ", "");
            //Converts each value as an indiviudal char.
            Char[] diagnosisTester = currentName.ToCharArray();

            //checks to make sure something was entered.
            if (diagnosisTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a diagnosis based on the test results.");
            }
        }
        /// <summary>
        /// Checks to make sure that a name is valid.
        /// </summary>
        /// <param name="name">The string to be checked.</param>
        /// <param name="position">The name 'position' such as a 'firs', 'middle', or 'last' so that
        /// the proper error message can be returned if an inappropriate name is entered.</param>
        public void validateName(String name, String position)
        {
            //Removes any spaces from the name to be added.
            String currentName = name.Replace(" ", "");
            //Converts each value as an indiviudal char.
            Char[] nameTester = currentName.ToCharArray();

            //checks to make sure something was entered.
            if (nameTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for " + position + " name.");
            }

            //Checks each value in the char array to make sure it is a letter.
            for (int currentPlace = 0; currentPlace < nameTester.Length; currentPlace++)
            {
                Char current = nameTester[currentPlace];
                if (Char.IsLetter(current) != true)
                {
                    throw new ArgumentException(position + " name must consist of all letters.");
                }
            }
        }

        /// <summary>
        /// This method validates the address.
        /// </summary>
        /// <param name="address">streetNumberToBeEntered</param>
        public void validateAddressStreetNumber(String streetNumber)
        {
            //Removes any spaces from the name to be added.
            String currentAddress = streetNumber.Replace(" ", "");
            //Converts each value as an indiviudal char.
            Char[] streetAdrNumber = currentAddress.ToCharArray();

            if (streetAdrNumber.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for Street Number.");
            }

            //Checks to make sure each digit is a number.
            for (int currentPlace = 0; currentPlace < streetNumber.Length; currentPlace++)
            {
                Char current = streetAdrNumber[currentPlace];
                if (Char.IsDigit(current) != true)
                {
                    throw new ArgumentException("Street number must consist of digits.");
                }
            }

        }

        /// <summary>
        /// This method makes sure a phone number is valid and aheres to proper format.
        /// </summary>
        /// <param name="number">Phone number to be entered.</param>
        public void validatePhoneNumber(String number)
        {
            //Removes excess spaces.
            String currentName = number.Replace(" ", "");
            //Converts each value as an indiviudal char.
            Char[] numberTester = currentName.ToCharArray();

            if (numberTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for Contact Number.");
            }

            //Checks the length to make sure the phone number had a valid length.
            if (numberTester.Length <= 9)
            {
                throw new ArgumentException("Must enter a valid phone number of at least 10 digits.");
            }

            if (numberTester.Length > 10)
            {
                throw new ArgumentException("Must enter a valid phone number no longer than 10 digits.");
            }

            //Checks to make sure each digit is a number.
            for (int currentPlace = 0; currentPlace < numberTester.Length; currentPlace++)
            {
                Char current = numberTester[currentPlace];
                if (Char.IsDigit(current) != true)
                {
                    throw new ArgumentException("Phone number entry must contain all numbers.");
                }
            }
        }

        /// <summary>
        /// Validate Pulse will take in a string and make sure 
        /// it adheres to the expected Pulse format.
        /// </summary>
        /// <param name="pressure">The string to check.</param>
        public void validatePulse(String pulses)
        {
            //Removes excess spaces.
            String currentName = pulses.Replace(" ", "");
            //Converts each value as an indiviudal char.
            Char[] pulsesTester = currentName.ToCharArray();

            if (pulsesTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for Pulse.");
            }

            //Checks to make sure each digit is a number.
            for (int currentPlace = 0; currentPlace < pulsesTester.Length; currentPlace++)
            {
                Char current = pulsesTester[currentPlace];
                if (Char.IsDigit(current) != true)
                {
                    throw new ArgumentException("Pulse must consist all numbers.");
                }
            }
        }

        /// <summary>
        /// Validate temperature will take in a string and make sure 
        /// it adheres to the expected temperature format.
        /// </summary>
        /// <param name="pressure">The string to check.</param>
        public void validateBodyTemperature(String temp)
        {
            //Removes excess spaces.
            String currentName = temp.Replace(" ", "");
            currentName = temp.Replace(".", "");
            //Converts each value as an indiviudal char.
            Char[] tempTester = currentName.ToCharArray();

            if (tempTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for Body Temperature.");
            }

            if (!temp.Contains(".") && temp.Length >= 3)
            {
                throw new ArgumentException("Must enter a valid value for Body Temperature.");
            }

            if (tempTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for Body Temperature.");
            }

            if (tempTester.Length > 5)
            {
                throw new ArgumentException("Must enter a valid value for Body Temperature.");
            }

            //Checks to make sure each digit is a number.
            for (int currentPlace = 0; currentPlace < tempTester.Length; currentPlace++)
            {
                Char current = tempTester[currentPlace];
                if (Char.IsDigit(current) != true)
                {
                    throw new ArgumentException("Body Temperature must consist all numbers in the format 'ddd.dd'.");
                }
            }
        }

        /// <summary>
        /// Validate blood pressure will take in a string and make sure 
        /// it adheres to the expected blood pressure format.
        /// </summary>
        /// <param name="pressure">The string to check.</param>
        public void validateBloodPressure(String pressure)
        {
            //Removes excess spaces.
            String currentName = pressure.Replace(" ", "");
            currentName = pressure.Replace("/", "");
            //Converts each value as an indiviudal char.
            Char[] pressureTester = currentName.ToCharArray();

             if (pressureTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for Blood Pressure.");
            }

            if(!pressure.Contains("/")){
                throw new ArgumentException("Blood Pressure must consist all numbers and '/'.");
            }

            if (pressureTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for Blood Pressure.");
            }

            if (pressureTester.Length >= 8)
            {
                throw new ArgumentException("Must enter a valid value for Blood Pressure.");
            }



            //Checks to make sure each digit is a number.
            for (int currentPlace = 0; currentPlace < pressureTester.Length; currentPlace++)
            {
                Char current = pressureTester[currentPlace];
                if (Char.IsDigit(current) != true)
                {
                    throw new ArgumentException("Blood Pressure must consist all numbers and '/'.");
                }
            }
        }

        /// <summary>
        /// Validates the time to make sure it adheres to the appropriate format.
        /// </summary>
        /// <param name="time">Time to check.</param>
        public void validateTime(String time)
        {
            if(!time.Contains(":")){
                throw new ArgumentException("Time must be in the the format hh:mm.");
            }

            if(time.Length >= 6){
                throw new ArgumentException("Time must be in the the format hh:mm.");
            }
            //Removes excess spaces.
            String currentName = time.Replace(" ", "");
            //Splits the time to text individual parts.
            String[] testTime = time.Split(':');

            currentName = time.Replace(":", "");
            //Converts each value as an indiviudal char.
            Char[] timeTester = currentName.ToCharArray();

            int hour = Int32.Parse(testTime[0]);
            int minutes = Int32.Parse(testTime[1]);

            if (timeTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for Time.");
            }

            if (hour >= 13)
            {
                throw new ArgumentException("Must enter a valid time for hours.");
            }

            if (minutes >= 60)
            {
                throw new ArgumentException("Must enter a valid time for minutes.");
            }

            //Checks the length to make sure the time has a valid length.
            if (timeTester.Length <= 3)
            {
                throw new ArgumentException("Must enter a valid time in the format hh:mm.");
            }

            if (timeTester.Length > 4)
            {
                throw new ArgumentException("Must enter a valid time in the format hh:mm.");
            }

            //Checks to make sure each digit is a number.
            for (int currentPlace = 0; currentPlace < timeTester.Length; currentPlace++)
            {
                Char current = timeTester[currentPlace];
                if (Char.IsDigit(current) != true)
                {
                    throw new ArgumentException("Time entry must consist of all numbers.");
                }
            }
        }

        /// <summary>
        /// Checks to validate test results to make sure it is one of the three
        /// acceptable test results. 
        /// </summary>
        /// <param name="testResult">The string to be tested.</param>
        public void validateTestResults(String testResult)
        {
            string check = testResult.ToLower().Trim();
            if (!check.Equals("positive") && !check.Equals("negative") && !check.Equals("inconclusive"))
            {
                throw new ArgumentException("Must enter a valid entry for the test result :" + "\n" + "Positive, negative, or inconclusive");
            }
        }
        /// <summary>
        /// Validates the respective IDs entered,
        /// </summary>
        /// <param name="ID">The ID to be checked.</param>
        /// <param name="currentIDChecking">Takes in a parameter to return message to the user if an error occurs.</param>
        public void validateID(String ID, String currentIDChecking)
        {
            //Removes any spaces from the name to be added.
            String currentName = ID.Replace(" ", "");
            //Converts each value as an indiviudal char.
            Char[] idTester = currentName.ToCharArray();

            //checks to make sure something was entered.
            if (idTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for " + currentIDChecking + " id.");
            }

            //Checks each value in the char array to make sure it is a letter.
            for (int currentPlace = 0; currentPlace < idTester.Length; currentPlace++)
            {
                Char current = idTester[currentPlace];
                if (Char.IsDigit(current) != true)
                {
                    throw new ArgumentException(currentIDChecking + " ID must consist of all digits.");
                }
            }
        }

        /// <summary>
        /// Checks to make sure that an entered date follows the appropriate format.
        /// </summary>
        /// <param name="date">The date to be checked</param>
        public void validateDate(String date)
        {
            //Configures the date to test the string values.
            String currentName = date.Replace(" ", "");
            String[] dateTester = date.Split('-');

            //Seperates the date into chars to test each individual value.
            String currentNameToChar = currentName.Replace("-", "");
            Char[] testDate = currentNameToChar.ToCharArray();


            if (testDate.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for Date.");
            }

            //Checks to make sure the date is formatted correctly, or at least the proper number of sections.
            if (dateTester.Length < 3 || dateTester.Length > 3)
            {
                throw new ArgumentException("Must enter a valid date in the format yyyy-mm-dd.");
            }

            //Make sure each character is a digit.
            for (int currentPlace = 0; currentPlace < testDate.Length; currentPlace++)
            {
                Char current = testDate[currentPlace];
                if (Char.IsDigit(current) != true)
                {
                    throw new ArgumentException("Date must contain only digits and the character '/'.");
                }
            }

            //Gets the specific string section of each section of the date.
            string year = dateTester[0];
            string month = dateTester[1];
            string day = dateTester[2];

            Int32 yearNumber = Convert.ToInt32(year);
            Int32 monthNumber = Convert.ToInt32(month);
            Int32 dayNumber = Convert.ToInt32(day);

            if (year.Length > 4 || year.Length < 4)
            {
                throw new ArgumentException("A year must consist of four characters. (yyyy)");
            }

            if (month.Length > 2 || month.Length < 2)
            {
                throw new ArgumentException("A month must consist of two characters. (mm)");
            }

            if (day.Length > 2 || day.Length < 2)
            {
                throw new ArgumentException("A day must consist of two characters. (dd)");
            }

            if (yearNumber >= 2012 || yearNumber < 1900)
            {
                throw new ArgumentException("Must enter a valid date for the year.");
            }

            if (monthNumber > 12 || monthNumber <= 0)
            {
                throw new ArgumentException("Must enter a valid date for the month.");
            }

            if (dayNumber > 31 || dayNumber <= 0)
            {
                throw new ArgumentException("Must enter a valid date for the day.");
            }
        }

        /// <summary>
        /// This methods makes sure that the Street Zip Code is valid and adheres to proper format
        /// </summary>
        /// <param name="Zip">Street Zip Code</param>
        public void validateStreetZipCode(String Zip)
        {
            String currentZip = Zip.Replace(" ", "");

            Char[] testZip = currentZip.ToCharArray();

            //Checks to see if the street zip code acutally has numbers
            if (testZip.Length <= 0)
            {
                throw new ArgumentException("Must enter a value for Street Zip Code");
            }
            //Checks to see if the street zip code is less than 5
            if (testZip.Length <= 4)
            {
                throw new ArgumentException("Zip Code must consist of 5 digits.");
            }
            //checks to see if the street zip code is greater than 5
            if (testZip.Length >= 6)
            {
                throw new ArgumentException("Zip Code must consist of 5 digits.");
            }

            //Checks to make sure each digit is a number.
            for (int currentPlace = 0; currentPlace < testZip.Length; currentPlace++)
            {
                Char current = testZip[currentPlace];
                if (Char.IsDigit(current) != true)
                {
                    throw new ArgumentException("Street Zip Code entry must contain all numbers.");
                }
            }
        }

        /// <summary>
        /// Checks to make sure the state initial is valid and letters.
        /// </summary>
        /// <param name="StateInitial">The state intial to be checked</param>
        public void validateStateIntital(String StateInitial)
        {
            string currentStateInitial = StateInitial.Replace(" ", "");
            Char[] stateTester = currentStateInitial.ToCharArray();

            //Checks to make sure the State intial is greater than zero.
            if (stateTester.Length <= 0)
            {
                throw new ArgumentException("Must enter a proper value for State Initial");
            }
            //Checks to make sure the state intial is gearter than 1
            if (stateTester.Length <= 1)
            {
                throw new ArgumentException("State Initial must be a two letters. ");
            }
            //Checks to make sure the State initial is less than 2
            if (stateTester.Length >= 3)
            {
                throw new ArgumentException("State Initial must be a two letters.");
            }

            //Checks each value in the char array to make sure it is a letter.
            for (int currentPlace = 0; currentPlace < stateTester.Length; currentPlace++)
            {
                Char current = stateTester[currentPlace];
                if (Char.IsLetter(current) != true)
                {
                    throw new ArgumentException(" State initial must consist of all letters.");
                }
            }
        }

    }

      
}
